package com.youi.web;

import com.google.common.collect.Maps;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.MarketAdvertisement;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.service.MarketAdvertisementService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @Author: winty
 * @Date: 2020/5/15 9:14
 */
@RestController
public class MarketAdvertisementController extends HttpRequestManage {

    @Autowired
    private MarketAdvertisementService marketAdvertisementService;

    /**
     * 获取首页广告轮播
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/listBannerAdvertisement")
    public String listBannerAdvertisement(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Map<String, Object> params = Maps.newHashMap();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.STATUS, 1);
        //1-首页banner,2-商品插播
        params.put("position", 1);
        params.put("currentDate", new Date());
        params.put("offset", 0);
        params.put("rows", 6);

        List<MarketAdvertisement> advertisementList = marketAdvertisementService.listAdvertisementByParam(params);
        if (CollectionUtils.isEmpty(advertisementList)) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", advertisementList);
    }


    /**
     * 获取商品插播广告
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @Deprecated
    @RequestMapping(value = "/listGoodsAdvertisement")
    public String listGoodsAdvertisement(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Map<String, Object> params = Maps.newHashMap();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.STATUS, 1);
        //1-首页banner,2-商品插播
        params.put("position", 2);
        params.put("gmtBegin", new Date());
        params.put("gmtEnd", new Date());

        List<MarketAdvertisement> advertisementList = marketAdvertisementService.listAdvertisementByParam(params);
        if (CollectionUtils.isEmpty(advertisementList)) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", advertisementList);
    }
}
